package flst.ipiweb.struts.action.relai;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;

import flst.ipiweb.common.DeliveryRecipient;

public class RelayComparator implements Comparator<DeliveryRecipient> {

	private CompareType type;
	private static Collator COLLATOR = Collator.getInstance(Locale.FRENCH);

	public RelayComparator(CompareType t) {
		this.type = t;
	}

	@Override
	public int compare(DeliveryRecipient p1, DeliveryRecipient p2) {
		if (this.type == CompareType.NOM) {
			int c = COLLATOR.compare(p1.getName(), p2.getName());
			if (c == 0) {
				return COLLATOR.compare(p1.getAddress().getCity(), p2.getAddress().getCity());
			} else {
				return c;
			}
		} else {
			int c = COLLATOR.compare(p1.getAddress().getCity(), p2.getAddress().getCity());
			if (c == 0) {
				return COLLATOR.compare(p1.getName(), p2.getName());
			} else {
				return c;
			}
		}

	}

	public enum CompareType {
		NOM, VILLE;
	}
}
